import { BasicColumn, FormSchema } from "/@/components/Table"
import { render } from "/@/utils/common/renderUtils"
import { rules } from "/@/utils/helper/validator"
import { DescItem } from "/@/components/Description/index"
import { ChannelTypeEnum } from "./model"
import { JVxeTypes, JVxeColumn } from "/@/components/jeecg/JVxeTable/types"
import { useUserStore } from "/@/store/modules/user"
import { listDataGroupTree } from "/@/api/infra/dataGroup"
import { DataGroupBizType } from "/@/components/BizDataGroup/index"
import { PageEnum } from "/@/enums/pageEnum"
import { ConfigKeyEnum } from "/@/enums"
import { TenantType } from "/@/enums/bizEnum"
import { useIam } from '/@/hooks/web/useIam'
import dictEnum from "/@/enums/dictEnum"

const userStore = useUserStore()

export const columns: BasicColumn[] = [
  {
    title: "消息名称",
    dataIndex: "name",
    width: 260,
  },
  {
    title: "消息编码",
    dataIndex: "code",
    width: 260,
  },
  {
    title: "渠道类型",
    dataIndex: "channelList",
    align: "left",
    minWidth: 300,
    slot: "channelList",
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    field: "name",
    label: "名称",
    component: "Input",
  },
  {
    field: "receiverType",
    label: "接收者类型",
    component: "JDictSelectTag",
    componentProps: {
      allowClear: true,
      dictType: "msg_receiver_type",
      placeholder: "全部",
      chooseOptionLabel: "全部"
    },
  },
  {
    field: "groupId",
    label: "消息组",
    component: "ApiSelect",
    componentProps: ({ formActionType }) => {
      return {
        api: listDataGroupTree,
        params: {
          bizType: DataGroupBizType.MSG_PUSH_TASK,
        },
        allowClear: true,
        optionFilterProp: "label",
        showSearch: true,
        labelField: "name",
        valueField: "id",
        dropdownStyle: { maxHeight: "16vh", overflow: "auto" },
      }
    },
  },
  {
    label: "渠道类型",
    field: "channelType",
    component: "JDictSelectTag",
    defaultValue: [],
    componentProps: {
      dictType: "message_channel_type",
      showSearch: true,
      allowClear: true,
      placeholder: "全部",
      chooseOptionLabel: "全部"
    },
  }
]

export const selectSubNoticeSearchForm: FormSchema[] = [
  {
    field: "name",
    label: "名称",
    component: "Input",
  },
]

export const formSchema: FormSchema[] = [
  {
    field: "id",
    label: "id",
    component: "Input",
    show: false,
    required: false,
  },
  // {
  //   field: 'name',
  //   label: '名称',
  //   component: 'Input',
  //   required: true
  // },
  {
    field: "isShowSelectTask",
    label: "isShowSelectTask",
    component: "RadioGroup",
    show: false,
    required: false,
  },
  {
    field: "name",
    label: "消息名称",
    dynamicDisabled: ({ values }) => {
      return !useIam().isAppHubOrMgt()
    },
    component: "Input",
    required: true,
  },
  {
    field: "code",
    label: "消息编码",
    dynamicDisabled: ({ values }) => {
      return !!values.id && values.id != ''
    },
    component: "Input",
    required: true,
  },

  {
    field: "receiverType",
    label: "接收者类型",
    defaultValue: "1",
    // helpMessage: '如果是会员通知, 则在代码中可以为任意用户推送',
    component: "JDictSelectTag",
    componentProps: {
      type: "radio",
      dictType: "msg_receiver_type",
    },
    dynamicDisabled: ({ values }) => {
      return !useIam().isAppHubOrMgt()
    },
  },

  {
    label: "消息功能组",
    field: "groupId",
    component: "ApiSelect",
    dynamicDisabled: ({ values }) => {
      return !useIam().isAppHubOrMgt()
    },
    componentProps: {
      api: listDataGroupTree,
      params: {
        bizType: DataGroupBizType.MSG_PUSH_TASK,
      },
      optionFilterProp: "label",
      showSearch: true,
      labelField: "name",
      valueField: "id",
      dropdownStyle: { maxHeight: "16vh", overflow: "auto" },
    },
  },

  {
    label: "渠道类型",
    field: "channelTypes",
    required: true,
    component: "JDictSelectTag",
    componentProps: {
      mode: "multiple",
      type: "list",
      dictType: "message_channel_type",
      showSearch: true,
    },
  },

  {
    field: "status",
    label: "状态",
    defaultValue: true,
    required: true,
    component: "RadioGroup",
    componentProps: {
      options: [
        { label: "启用", value: 1 },
        { label: "禁用", value: 0 },
      ],
    },
  },

  {
    label: "备注",
    field: "remark",
    dynamicDisabled: ({ values }) => {
      return !useIam().isAppHubOrMgt()
    },
    component: "InputTextArea",
    componentProps: {
      placeholder: "请输入备注",
      rows: 5,
    },
  },
]

export const channelColumns: BasicColumn[] = [
  {
    title: "渠道类型",
    dataIndex: "channelTypeName",
    width: 100,
  },
  {
    title: "渠道名称",
    dataIndex: "channelName",
    width: 160,
  },
]

export const channelConfigSchema: FormSchema[] = [
  {
    field: "status",
    label: "状态",
    defaultValue: "0",
    helpMessage: "被禁用将不会推送消息",
    required: true,
    component: "JDictSelectTag",
    componentProps: {
      type: "radio",
      dictType: dictEnum.DataAvailable,
    },
  },
  {
    field: "customFlag",
    label: "自定义",
    helpMessage: "如果关闭自定义, 则采用系统或者主租户的渠道配置",
    required: true,
    show: ({ values }) => {
      return !useIam().isAppHubOrMgt()
    },
    component: "RadioGroup",
    componentProps: {
      options: [
        {
          label: "是",
          value: true,
        },
        {
          label: "否",
          value: false,
        },
      ],
    },
  },
  {
    field: "title",
    label: "标题",
    required: false,
    show: ({ values }) => {
      return values.customFlag && (values.channelType === ChannelTypeEnum.EMAIL || values.channelType === ChannelTypeEnum.PC)
    },
    component: "Input"
  },
  {
    field: "templateId",
    label: "模板ID",
    helpMessage: "第三方模板id, eg: 微信小程序/微信公众后消息模板id",
    show: ({ values }) => {
      return values.customFlag && values.channelType !== ChannelTypeEnum.EMAIL &&
        values.channelType !== ChannelTypeEnum.PC &&
        values.channelType !== ChannelTypeEnum.EVENT_BUS
    },
    dynamicDisabled: ({ values }) => {
      return userStore.getSelectTenant.tenantType === TenantType.SUB
    },
    required: ({ values }) =>
      values.channelType !== ChannelTypeEnum.EMAIL &&
      values.channelType !== ChannelTypeEnum.PC &&
      values.channelType !== ChannelTypeEnum.EVENT_BUS,
    component: "Input",
  },
  {
    // 小程序或者PC推送才拥有
    field: "pageUrl",
    label: "页面地址",
    helpMessage: [
      "1、点击推送的消息将会跳转到该地址",
      "2、支持占位符 ${h5Url} 来表示在系统中配置的H5域名",
      "3、如果指定了小程序appId, 则填写小程序地址",
    ],
    dynamicDisabled: ({ values }) => {
      return !useIam().isAppHubOrMgt()
    },
    required: false,
    show: ({ values }) => {
      return values.customFlag && (
        values.channelType === ChannelTypeEnum.PC ||
        values.channelType === ChannelTypeEnum.WX_MA ||
        values.channelType === ChannelTypeEnum.WX_MP
      )
    },
    component: "Input",
  },
  {
    field: "channelType",
    label: "channelType",
    show: false,
    component: "Input",
  },
  {
    // PC推送或者APP推送才拥有
    field: "mediaId",
    label: "媒体文件",
    helpMessage: "推送的时候会以链接形式推送给目标, 需要从素材库中选择",
    required: false,
    show: ({ values }) => {
      return values.customFlag && values.channelType === ChannelTypeEnum.PC
    },
    component: "MySelectMaterial",
  },
  {
    // 是否长连接转成短连接
    field: "convertToShortLink",
    label: "使用短连接",
    helpMessage: [
      "当推送的字段包含http或者https地址时, 会自动转成短连接",
    ],
    required: false,
    show: ({ values }) => {
      return values.customFlag && (values.channelType === ChannelTypeEnum.EMAIL || values.channelType === ChannelTypeEnum.SMS)
    },
    dynamicDisabled: ({ values }) => {
      return !useIam().isAppHubOrMgt()
    },
    component: "RadioGroup",
    componentProps: {
      options: [
        {
          label: "是",
          value: true,
        },
        {
          label: "否",
          value: false,
        },
      ],
    },
  },
  {
    // 是否跳转到小程序
    field: "isJumpMa",
    label: "关联小程序",
    helpMessage: [
      "1、点击推送的消息且指定了页面地址则跳转到小程序",
      `2、<a target='_blank' href='${PageEnum.CONFIG_CENTER}?configKey=${ConfigKeyEnum.WX_MA}'>去配置</a>, 也可以指定其他小程序appId`,
    ],
    required: false,
    show: ({ values }) => {
      return values.customFlag && values.channelType === ChannelTypeEnum.WX_MP
    },
    component: "RadioGroup",
    componentProps: {
      options: [
        {
          label: "是",
          value: true,
        },
        {
          label: "否",
          value: false,
        },
      ],
    },
  },
  {
    // 跳转到的小程序appId
    field: "maAppId",
    label: "小程序appId",
    helpMessage: [
      "1、点击推送的消息且指定了页面地址则跳转到小程序",
      "2、指定后, 优先使用指定的小程序appId, 如果没有指定, 则使用系统中配置的小程序id",
    ],
    required: false,
    show: ({ values }) => {
      return values.customFlag && values.channelType === ChannelTypeEnum.WX_MP
    },
    component: "Input",
  },
  {
    field: "content",
    label: "模板内容",
    helpMessage: "除了邮件, 其他渠道仅作为在推送历史中消息查看",
    slot: "contentEdit",
    component: "Input",
    show: ({ values }) => {
      return values.customFlag
    },
    dynamicDisabled: ({ values }) => {
      return !useIam().isAppHubOrMgt()
    },
  },
]

/**
 * 消息接收人
 */
export const receiverColumns: BasicColumn[] = [
  {
    title: "姓名",
    dataIndex: "name",
    width: 100,
  },
  {
    title: "手机号",
    dataIndex: "phone",
    slots: { customRender: "phone" },
    width: 120,
  },
  {
    title: "邮箱",
    dataIndex: "email",
    slots: { customRender: "email" },
    width: 150,
  },
  {
    title: "备注",
    dataIndex: "remark",
    width: 100,
  },
]
